package com.ut.uttools.aop;


import com.ut.uttools.annos.Logger;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import javax.servlet.http.HttpServletRequest;

/**
 * 操作日志AOP
 * @author yin_q
 */
@Aspect
@Slf4j
public class LogAop {

	// 起始时间
	private long startTime;
	
	// 结束时间
	private long returnTime;
	
	@Autowired
	private HttpServletRequest request;

	@Pointcut("@annotation(logger)")
    public void handleLog(Logger logger){}

	/**
	 * 前置通知
	 * @param joinPoint
	 * @param logger
	 */
	@Before("handleLog(logger)")
	public void doBefore(JoinPoint joinPoint, Logger logger){
		log.info("记录操作日志：{}", logger.value());
	}

	/**
	 * 接口后置通知
	 * @param logger
	 * @param returnValue
	 */
	@AfterReturning(value = "handleLog(logger)", returning = "returnValue")
	public void doAfter(Logger logger, Object returnValue){
		System.out.println("接口后置通知:"+ returnValue );
	}

	/**
	 * 异常通知
	 * @param logger
	 * @param e
	 */
	@AfterThrowing(value = "handleLog(logger)", throwing = "e")
	public void doAfterThrowing(Logger logger, Throwable e){
		System.out.println("异常通知");
	}
}
